package com.shenghua.utils;


import java.net.SocketException;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

//import org.apache.log4j.Logger;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;


public class Mail {
	
	//private Logger log = Logger.getLogger(Mail.class);
	public  boolean sendMail(JavaMailSender mailSender,String MimeMessageHelperTo) {
		boolean send =true;
		try {
			
//		JavaMailSender mailSender; 
//		JavaMailSenderImpl javaMail=new JavaMailSenderImpl();
//		javaMail.setHost("smtp.qq.com");
//		javaMail.setUsername("3383548808");
//		javaMail.setPassword("vvsllclgomqfidfc");
//		javaMail.setProtocol("smtp");
//		javaMail.setDefaultEncoding("utf-8");
//		//设置SMTP服务器要的用户验证
//		javaMail.setJavaMailProperties(new JavaMailSenderImpl().getJavaMailProperties());
//		Properties p=	new Properties();
//		p.setProperty("mail.smtp.auth", "mail.smtp.auth");
//		javaMail.setJavaMailProperties(p);
//		Properties p2=	new Properties();
//		p2.setProperty("mail.smtp.ssl.enable", "true");
//		javaMail.setJavaMailProperties(p2);
//		mailSender=javaMail;

//		param = paramService.getParam();
		/*param.setEmailFrom("3383548808@qq.com");
		param.setMallChineseName("上号家商城");
		param.setSoftName("EasyMall");*/
//		if(param.getEmailFrom()== null || "".equals(param.getEmailFrom())){
//			param.setEmailFrom("1554420442@qq.com");
//		}
//		log.info("当前邮箱发送者：");
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
		helper.setFrom("");
		helper.setTo(MimeMessageHelperTo);
		//helper.setTo(new String[]{"787559203@qq.com"});
		StringBuffer sb = new StringBuffer();
		String ip="localhost";
		try {
			String ipb=Iptool.localIP();//本地服务器ip
			if(ipb!=null&&ipb!="")
				ip=ipb;
		} catch (SocketException e1) {
			e1.printStackTrace();
//			log.debug(e1.getMessage());
			send = false;
		}
		sb.append("<HTML> <BODY>Hi！"+MimeMessageHelperTo+"：<br/>你正在尝试注册！ <br/>如果这是你的操作,请  <a href='http://"+
		ip+":8080/WbusiManage/reg/cdMail?mailName="+MimeMessageHelperTo+"'>单击我</a>完成后续操作！<br/>如果不是你的操作，请忽略此操作<br/> <BR> <HR> <FONT SIZE=2> 无论怎么样，我都会努力做好！ </FONT> <br/><br/>微霸产品经理 </BODY> </HTML>");
		helper.setSubject("激活您的账号");
		helper.setText(sb.toString(), true);
		try {
			mailSender.send(message);
		} catch (Exception e) {
			e.printStackTrace();
//			log.debug(e.getMessage());
			send=false;
		}
		} catch (MessagingException e) {
			e.printStackTrace();
//			log.debug(e.getMessage());
			send=false;
		}
		return send;
	}
	// 验证成功则去掉邮箱后面的“？”
	public  boolean sendMailForValidate(JavaMailSender mailSender,String MimeMessageHelperTo) {
		boolean send =true;
		try {
//		log.info("当前邮箱发送者：");
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
		helper.setFrom("");
		helper.setTo(MimeMessageHelperTo);
		//helper.setTo(new String[]{"787559203@qq.com"});
		StringBuffer sb = new StringBuffer();
		String ip="localhost";
		try {
			String ipb=Iptool.localIP();//本地服务器ip
			if(ipb!=null&&ipb!="")
				ip=ipb;
		} catch (SocketException e1) {
			e1.printStackTrace();
//			log.debug(e1.getMessage());
			send = false;
		}
		sb.append("<HTML> <BODY>Hi！"+MimeMessageHelperTo+"：<br/>祝贺你已成功注册" + 
		 "！现在尝试验证邮箱， <br/>如果这是你的操作,请  <a href='http://"+ip+
		":8080/WbusiManage/reg/validateMail?mailName="+MimeMessageHelperTo+
		"'>单击我</a>完成后续操作！<br/>如果不是你的操作，请忽略此操作<br/> " +
		"<BR> <HR> <FONT SIZE=2> 商路已通，让我们一起前行！ </FONT> <br/>" +
		"<br/>产品经理 </BODY> </HTML>");
		helper.setSubject("激活您的账号");
		helper.setText(sb.toString(), true);
		try {
			mailSender.send(message);
		} catch (Exception e) {
			e.printStackTrace();
//			log.debug(e.getMessage());
			send=false;
		}
		} catch (MessagingException e) {
			e.printStackTrace();
//			log.debug(e.getMessage());
			send=false;
		}
		return send;
	}
	
	// 验证成功则去掉邮箱后面的“？”
	public  boolean sendMailForGetBackPwd(JavaMailSender mailSender,String MimeMessageHelperTo,String phone) {
		boolean send =true;
		try {
		/*param.setEmailFrom("3383548808@qq.com");
		param.setMallChineseName("上号家商城");
		param.setSoftName("EasyMall");*/
//		log.info("当前邮箱发送者：");
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
		helper.setFrom("");
		helper.setTo(MimeMessageHelperTo);
		//helper.setTo(new String[]{"787559203@qq.com"});
		StringBuffer sb = new StringBuffer();
		String ip="localhost";
		try {
			String ipb=Iptool.localIP();//本地服务器ip
			if(ipb!=null&&ipb!="")
				ip=ipb;
		} catch (SocketException e1) {
			e1.printStackTrace();
//			log.debug(e1.getMessage());
			send = false;
		}
		sb.append("<HTML> <BODY>Hi！"+MimeMessageHelperTo+"：<br/>" + 
		"现在接受您找回密码的请求！<br/>如果这是你的操作,请  <a href='http://"+ip+
		":8080/WbusiManage/reg/getpwdbyemail?key="+DesUtil.encrypt(phone)+
		"'>单击我</a>完成后续操作！<br/>如果不是你的操作，请忽略此操作<br/> " +
		"<BR> <HR> <FONT SIZE=2> 感谢您一向的支持！ </FONT> <br/>" +
		"<br/>商城助手 </BODY> </HTML>");
		helper.setSubject("找回您的登录密码");
		helper.setText(sb.toString(), true);
		try {
			mailSender.send(message);
		} catch (Exception e) {
			e.printStackTrace();
//			log.debug(e.getMessage());
			send=false;
		}
		} catch (MessagingException e) {
			e.printStackTrace();
//			log.debug(e.getMessage());
			send=false;
		}
		return send;
	}
}
